<?php
namespace Admin\Controller;
/**
 * 商品管理
 * @author Dinghua
 *
 */
class GoodsController extends AdminController{
	
	/**
	 * 商品列表
	 */
	public function index() {
		$map = array();
		$map['status']  = array('in','0,1');
		$title = I('get.title');
		if ($title) {
			$map['title'] = array('like',"%$title%");;
		}
		$list = $this->lists('Goods', $map, 'id DESC');
		
		$this->assign('_list',$list);
		$this->meta_title = '商品列表';
		
		// 记录当前列表页的cookie
		Cookie('__forward__',$_SERVER['REQUEST_URI']);
		
		$this->display();
	}
	
	
	/**
	 * 新增商品
	 */
	public function add() {
		$this->meta_title = '新增商品';
		$this->display();
	}
	
	
	/**
	 * 编辑商品
	 * @param string $id 商品ID
	 */
	public function edit($id = NULL) {
		if (empty($id)) $this->error('参数错误');
		$info = D('Goods')->info($id);
		$this->assign('info', $info);
		$this->meta_title = '编辑商品';
	
		$this->display('add');
	}
	
	
	/**
	 * 更新或新增一条数据
	 */
	public function update(){
	
		$Goods = D('Goods');
		$res = $Goods->update();
		if(!$res){
			$this->error($Goods->getError());
		}else{
			$this->success($res['id']?'更新成功':'新增成功', Cookie('__forward__'));
		}
	}

	/**
	 * 来自ajax修改
	 */
	public function ajaxPost() {
		$id = I('id');
		$val = I('val');
		$field = I('field');
		$map = array('id' => $id);
		$Goods = M('Goods');
		$data = array($field => $val);
		$res = $Goods->where($map)->save($data);
		if(!$res){
			$this->error($Goods->getError());
		}else{
			$this->success('更新成功', Cookie('__forward__'));
		}
	}
	
	/**
	 * 商品列表回收站
	 */
	public function recycle() {
		$map = array();
		$map['status']  = '-1';
		$title = I('get.title');
		if ($title) {
			$map['title'] = array('like',"%$title%");;
		}
		$list = $this->lists('Goods', $map);
	
		$this->assign('_list',$list);
		$this->meta_title = '商品列表回收站';
	
		// 记录当前列表页的cookie
		Cookie('__forward__',$_SERVER['REQUEST_URI']);
		$this->display();
	}
	
}